﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Document_Management
{
    public partial class frmJiaoZheng : Form
    {
        static string connectionString = ConfigurationManager.ConnectionStrings["庫存管理"].ConnectionString;
        SqlConnection con = new SqlConnection(connectionString);
        string jiaozheng, tbz0, tbz1, tbz2, tbz3, tbz4, tbz5, tbz6, tbz7, tbz8, tbz9, tbzn1, tbzn2, tbzn3, tbzn4, tbzn5;
        bool b量具Search, b量具Insert, b量具Edit, b量具Del;
        public frmJiaoZheng(string jiaozheng, string tbz0, string tbz1, string tbz2, string tbz3, string tbz4, string tbz5, string tbz6, string tbz7, string tbz8, string tbz9, string tbzn1, string tbzn2, string tbzn3, string tbzn4, string tbzn5, bool b量具Search,bool b量具Insert,bool b量具Edit,bool b量具Del)
        {
            InitializeComponent();
            this.tbz0 = tbz0;
            this.tbz1 = tbz1;
            this.tbz2 = tbz2;
            this.tbz3 = tbz3;
            this.tbz4 = tbz4;
            this.tbz5 = tbz5;
            this.tbz6 = tbz6;
            this.jiaozheng = jiaozheng;
            this.tbz7 = tbz7;
            this.tbz8 = tbz8;
            this.tbz9 = tbz9;
            this.tbzn1 = tbzn1;
            this.tbzn2 = tbzn2;
            this.tbzn3 = tbzn3;
            this.tbzn4 = tbzn4;
            this.tbzn5 = tbzn5;
            this.b量具Search = b量具Search;
            this.b量具Insert = b量具Insert;
            this.b量具Edit = b量具Edit;
            this.b量具Del = b量具Del;
        }

        private void frmJiaoZheng_Load(object sender, EventArgs e)
        {
            txt量具狀態.Enabled = false;
            txt量具狀態.Text = "校正";

            // goi du lieu lay san pham dung
            if (jiaozheng == "1")
            {
                btn_Save.Text = "存檔";
                label1.Text = "量具校正";
                txt_量具編號.Text = tbz1;//tb1 0
                txt_Name量具.Text = tbz3; //tb4 3
                txt_規格型式.Text = tbz4;//cb1 1
                txt_存放地點.Text = tbz5; //tb3 2
                txt_測量範圍.Text = tbz6; //cb2  4
            }
            // goi du lieu Tra san pham ve kho
            if (jiaozheng == "2")
            {
                label1.Text = "量具有效";
                btn_Save.Text = "有效";

                txt_量具編號.Text = tbz1;//tb1 0
                txt量具狀態.Text = tbz2;
                txt_Name量具.Text = tbz3; //tb4 3
                txt_規格型式.Text = tbz4;//cb1 1
                txt_存放地點.Text = tbz5; //tb3 2
                txt_測量範圍.Text = tbz6; //cb2  4
                txt數量.Text = "";
                dateTimePicker_from_有效日期.Text = tbz8;
                dateTimePicker_to_有效日期.Text = tbz9;
                dateTimePicker_from_檢定日期.Text = tbzn1;
                dateTimePicker_to_檢定日期.Text = tbzn2;
                txt_檢定機構.Text = tbzn3;
                txt受理人代號.Text = tbzn4;
                txt_note.Text = tbzn5;
            }
        }

        private void btn領用紀錄_Click(object sender, EventArgs e)
        {
            frmJiaozhengjilu frm = new frmJiaozhengjilu(b量具Search, b量具Insert, b量具Edit, b量具Del);
            frm.ShowDialog();
        }

        private void btn_Cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        
        private void txt_量具編號_TextChanged(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(connectionString);
            try
            {
                con.Open();
                String temp = "";
                string strcmd = String.Format("select * from 量具訊息 where 量具編號=N'{0}'", txt_量具編號.Text);
                SqlCommand cmd = new SqlCommand(strcmd, con);
                var x = cmd.ExecuteReader();
                //int icount=0;
                while (x.Read()) temp = x[1].ToString();
                if (temp == "") lbName量具編號.Text = "";
                else lbName量具編號.Text = temp;
                con.Close();
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); }
        }

        private void txt_存放地點_TextChanged(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(connectionString);
            try
            {
                con.Open();
                String temp = "";
                string strcmd = String.Format("select * from 存放地點訊息 where 存放地點代號=N'{0}'", txt_存放地點.Text);
                SqlCommand cmd = new SqlCommand(strcmd, con);
                var x = cmd.ExecuteReader();
                //int icount=0;
                while (x.Read()) temp = x[1].ToString();
                if (temp == "") lbName存放地點.Text = "";
                else lbName存放地點.Text = temp;
                con.Close();
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); }
        }

        private void txt受理人代號_TextChanged(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(connectionString);
            try
            {
                con.Open();
                String temp = "";
                string strcmd = String.Format("select * from 校正_受理人 where 受理人代號=N'{0}'", txt受理人代號.Text);
                SqlCommand cmd = new SqlCommand(strcmd, con);
                var x = cmd.ExecuteReader();
                //int icount=0;
                while (x.Read()) temp = x[1].ToString();
                if (temp == "") lbName受理人代號.Text = "";
                else lbName受理人代號.Text = temp;
                con.Close();
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); }
        }

        private void btnSearch受理人代號_Click(object sender, EventArgs e)
        {
            frmSearchJiaoZhengShouliRen frm = new frmSearchJiaoZhengShouliRen(txt受理人代號);
            frm.ShowDialog();
        }

        private void btn_Save_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(connectionString);
            if (!b量具Insert)
            {
                MessageBox.Show("很抱歉，您沒有權限！", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            else
            {
                try
                {
                    // lay san pham su dung
                    if (jiaozheng == "1")
                    {
                        if (txt_量具編號.Text == "")
                        {
                            MessageBox.Show("請您輸入量具編號", "錯誤", MessageBoxButtons.OK);
                            txt_量具編號.Focus();
                            return;
                        }
                        if (txt量具狀態.Text == "")
                        {
                            MessageBox.Show("請您輸入量具狀態", "錯誤", MessageBoxButtons.OK);
                            txt量具狀態.Focus();
                            return;
                        }
                        if (txt數量.Text == "")
                        {
                            MessageBox.Show("請您輸入領用數量", "錯誤", MessageBoxButtons.OK);
                            txt數量.Focus();
                            return;
                        }
                        if (dateTimePicker_from_檢定日期.Text == "")
                        {
                            MessageBox.Show("請您輸入檢定日期", "錯誤", MessageBoxButtons.OK);
                            dateTimePicker_from_檢定日期.Focus();
                            return;
                        }
                        if (dateTimePicker_from_有效日期.Text == "")
                        {
                            MessageBox.Show("請您輸入有效日期", "錯誤", MessageBoxButtons.OK);
                            dateTimePicker_from_有效日期.Focus();
                            return;
                        }

                        if (txt受理人代號.Text == "")
                        {
                            MessageBox.Show("請您輸入受理人代號", "錯誤", MessageBoxButtons.OK);
                            txt受理人代號.Focus();
                            return;
                        }

                        if (txt_檢定機構.Text == "")
                        {
                            MessageBox.Show("請您輸入檢定機構", "錯誤", MessageBoxButtons.OK);
                            txt_檢定機構.Focus();
                            return;
                        }
                        // kiem tra thoi gian
                        DateTime a = DateTime.Parse(dateTimePicker_from_檢定日期.Text);
                        DateTime b = DateTime.Parse(dateTimePicker_to_檢定日期.Text);
                        if (a < b)
                        {
                            MessageBox.Show("請您輸入檢定日期 >= 檢定日期", "錯誤", MessageBoxButtons.OK);
                            dateTimePicker_to_檢定日期.Focus();
                            return;
                        }

                        DateTime c = DateTime.Parse(dateTimePicker_from_有效日期.Text);
                        DateTime d = DateTime.Parse(dateTimePicker_to_有效日期.Text);
                        if (d < c)
                        {
                            MessageBox.Show("請您輸入有效日期 >= 有效日期", "錯誤", MessageBoxButtons.OK);
                            dateTimePicker_to_有效日期.Focus();
                            return;
                        }
                        // kiem tra nhap shuliang nho hon so luong trong kho
                        SqlCommand sqlcmd = new SqlCommand();
                        sqlcmd.Connection = con;
                        con.Open();
                        sqlcmd.CommandText = "select 数量 from 量具訊息 where 量具編號=" + "N'" + tbz1 + "'";
                        sqlcmd.CommandType = CommandType.Text;
                        var icount = sqlcmd.ExecuteScalar();
                        int inumdel;
                        bool k = int.TryParse(txt數量.Text, out inumdel);
                        int istocknumber = int.Parse(icount.ToString());
                        if (!k || inumdel > int.Parse(icount.ToString()))
                        {
                            MessageBox.Show("請您輸入校正數量量具要刪除(int,<" + istocknumber + ")", "錯誤", MessageBoxButtons.OK);
                            txt數量.Focus();
                            //return;
                        }
                        else if (istocknumber == 0)
                        {
                            MessageBox.Show("不能校正", "錯誤", MessageBoxButtons.OK);
                        }
                        else
                        {
                            // tien hanh inset du lieu vao bang lingyong
                            DialogResult answer = MessageBox.Show("您確定要出入" + int.Parse(txt數量.Text) + "量具嗎？", "警報", MessageBoxButtons.YesNo);
                            if (answer == DialogResult.Yes)
                            {
                                //automation inset
                                string sp_insert = "sp_校正_新增";
                                SqlCommand cmd = new SqlCommand(sp_insert, con);
                                cmd.CommandType = CommandType.StoredProcedure;
                                cmd.Parameters.AddWithValue("@量具編號", txt_量具編號.Text);
                                cmd.Parameters.AddWithValue("@量具狀態", txt量具狀態.Text);
                                cmd.Parameters.AddWithValue("@量具名稱", txt_Name量具.Text);
                                cmd.Parameters.AddWithValue("@規格型號", txt_規格型式.Text);
                                cmd.Parameters.AddWithValue("@存放地點", txt_存放地點.Text.ToString());
                                cmd.Parameters.AddWithValue("@測量範圍", txt_測量範圍.Text.ToString());
                                cmd.Parameters.AddWithValue("@數量", Int32.Parse(txt數量.Text));
                                cmd.Parameters.AddWithValue("@有效日期From", DateTime.Parse(dateTimePicker_from_有效日期.Text));
                                cmd.Parameters.AddWithValue("@有效日期To", DateTime.Parse(dateTimePicker_to_有效日期.Text));
                                cmd.Parameters.AddWithValue("@檢定日期From", DateTime.Parse(dateTimePicker_from_檢定日期.Text));
                                cmd.Parameters.AddWithValue("@檢定日期To", DateTime.Parse(dateTimePicker_to_檢定日期.Text));
                                cmd.Parameters.AddWithValue("@檢定機構", txt_檢定機構.Text);
                                cmd.Parameters.AddWithValue("@受理人代號", txt受理人代號.Text.ToString());
                                cmd.Parameters.AddWithValue("@備註", txt_note.Text);
                                cmd.ExecuteNonQuery();

                                //ling yong liang ju
                                string delete = tbz1;
                                //string delete1 = tbl6;
                                string sp_increment = "sp_量具訊息_更新_-n";// tru so luong san pham trong kho khi linh yong
                                SqlCommand sqlincrement = new SqlCommand();
                                sqlincrement.Connection = con;
                                sqlincrement.CommandType = CommandType.StoredProcedure;
                                sqlincrement.CommandText = sp_increment;
                                sqlincrement.Parameters.AddWithValue("@量具編號", delete);
                                //sqlincrement.Parameters.AddWithValue("@量具狀態", delete1);
                                sqlincrement.Parameters.AddWithValue("@n", int.Parse(txt數量.Text));
                                sqlincrement.ExecuteNonQuery();

                                //string dele = "Select 數量 from 領用";

                                MessageBox.Show("領用成功");
                                con.Close();
                                this.Close();
                                frmJiaozhengjilu frm = new frmJiaozhengjilu(b量具Search, b量具Insert, b量具Edit, b量具Del);
                                frm.ShowDialog();
                                this.Close();
                            }
                            else return;
                        }
                    }
                    //tra san pham ling yong
                    if (jiaozheng == "2")
                    {
                        //==============================================================================
                        if (txt數量.Text == "")
                        {
                            MessageBox.Show("請您輸入修理數量", "錯誤", MessageBoxButtons.OK);
                            txt數量.Focus();
                            return;
                        }
                        int check = int.Parse(txt數量.Text);
                        if (check <= 0)
                        {
                            MessageBox.Show("不能有效數量<=0", "錯誤", MessageBoxButtons.OK);
                            txt數量.Focus();
                            return;
                        }
                        else
                        {
                            // kiem tra so luong tra nho hon so luong ling yong
                            SqlCommand sqlcmd = new SqlCommand();
                            sqlcmd.Connection = con;
                            con.Open();
                            sqlcmd.CommandText = "select 數量 from 校正 where 項次=" + "N'" + tbz0 + "'";
                            sqlcmd.CommandType = CommandType.Text;
                            var icount = sqlcmd.ExecuteScalar();
                            int inumdel;
                            bool k = int.TryParse(txt數量.Text, out inumdel);
                            int istocknumber = int.Parse(icount.ToString());
                            if (!k || inumdel > int.Parse(icount.ToString()))
                            {
                                MessageBox.Show("請您輸入校正數量量具要刪除(int,<" + istocknumber + ")", "錯誤", MessageBoxButtons.OK);
                                txt數量.Focus();
                                return;
                            }
                            if (inumdel == istocknumber)//delete from database
                            {
                                DialogResult answer1 = MessageBox.Show("您確定要出入" + inumdel + "校正量具碼", "警報", MessageBoxButtons.YesNo);
                                if (answer1 == DialogResult.Yes)
                                {
                                //DialogResult answer2 = MessageBox.Show("Bạn có muốn xóa A trong 修理紀錄 khi trả về 5 量具 va số lượng còn lại la bằng 0, hay ko ？", "警報", MessageBoxButtons.YesNo);
                                DialogResult answer2 = MessageBox.Show("當歸還" + inumdel + "量具及存校正紀錄的量具剩余數量是0的，要不要刪除這個量具？", "警報", MessageBoxButtons.YesNo);
                                if (answer2 == DialogResult.Yes)
                                {
                                    //con.Open();
                                    string sp_delete = "sp_校正_刪除";
                                    SqlCommand sqldelete = new SqlCommand();
                                    sqldelete.Connection = con;
                                    sqldelete.CommandType = CommandType.StoredProcedure;
                                    sqldelete.CommandText = sp_delete;
                                    sqldelete.Parameters.AddWithValue("@項次", tbz0);
                                    //MessageBox.Show("刪除成功");
                                    sqldelete.ExecuteNonQuery();
                                }
                                if (answer2 == DialogResult.No)
                                {
                                    string delete = tbz0;
                                    //string delete1 = tbl6;
                                    string sp_increment = "sp_校正_更新_-n"; // tru ling yong
                                    SqlCommand sqlincrement = new SqlCommand();
                                    sqlincrement.Connection = con;
                                    sqlincrement.CommandType = CommandType.StoredProcedure;
                                    sqlincrement.CommandText = sp_increment;
                                    sqlincrement.Parameters.AddWithValue("@項次", delete);
                                    //sqlincrement.Parameters.AddWithValue("@量具狀態", delete1);
                                    sqlincrement.Parameters.AddWithValue("@n", int.Parse(txt數量.Text));
                                    sqlincrement.ExecuteNonQuery();
                                }
                                    //Tra ve
                                    string sp_update = "sp_量具訊息_更新_+n"; // cong vao kho
                                    SqlCommand cmd1 = new SqlCommand(sp_update, con);
                                    cmd1.CommandType = CommandType.StoredProcedure;
                                    cmd1.Parameters.AddWithValue("@量具編號", txt_量具編號.Text);
                                    //cmd1.Parameters.AddWithValue("@量具狀態", txt量具狀態.Text.ToString());
                                    cmd1.Parameters.AddWithValue("@n", txt數量.Text);
                                    cmd1.ExecuteNonQuery();
                                    MessageBox.Show("歸還成功");
                                    this.Close();
                                    con.Close();
                                }
                                else
                                {
                                    return;
                                }
                            }
                            else
                            {
                                //====================================================================================
                                DialogResult answer = MessageBox.Show("您確定要出入" + int.Parse(txt數量.Text) + "量具嗎？", "警報", MessageBoxButtons.YesNo);
                                if (answer == DialogResult.Yes)
                                {
                                    //con.Open();
                                    //tra ve
                                    string delete = tbz0;
                                    //string delete1 = tbl6;
                                    string sp_increment = "sp_校正_更新_-n";// tru ling yong
                                    SqlCommand sqlincrement = new SqlCommand();
                                    sqlincrement.Connection = con;
                                    sqlincrement.CommandType = CommandType.StoredProcedure;
                                    sqlincrement.CommandText = sp_increment;
                                    sqlincrement.Parameters.AddWithValue("@項次", delete);
                                    //sqlincrement.Parameters.AddWithValue("@量具狀態", delete1);
                                    sqlincrement.Parameters.AddWithValue("@n", int.Parse(txt數量.Text));
                                    sqlincrement.ExecuteNonQuery();

                                    //Tra ve
                                    string sp_update = "sp_量具訊息_更新_+n"; // cong vao kho
                                    SqlCommand cmd1 = new SqlCommand(sp_update, con);
                                    cmd1.CommandType = CommandType.StoredProcedure;
                                    cmd1.Parameters.AddWithValue("@量具編號", txt_量具編號.Text);
                                    //cmd1.Parameters.AddWithValue("@量具狀態", txt量具狀態.Text.ToString());
                                    cmd1.Parameters.AddWithValue("@n", txt數量.Text);
                                    cmd1.ExecuteNonQuery();
                                    MessageBox.Show("歸還成功");
                                    con.Close();
                                    this.Close();
                                }
                                else return;
                            }
                        }
                    }
                }
                catch (Exception ex) { con.Close(); MessageBox.Show(ex.Message); }
                finally { con.Close(); }
            }
        }

        private void txt數量_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) || e.KeyChar == '.')
            {
                e.Handled = true;
                MessageBox.Show("請您輸入號碼", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

    }
}
